#include "DialogAddItem.h"
#include "components/GlobalComponents.h"
#include "tools/cJSON.h"
#include <QStandardItemModel>

DialogAddItem::DialogAddItem(QWidget * parent)
             : BaseDialog(parent)
             , m_pGroupKey(nullptr)
             , m_pGroupValue(nullptr)
             , m_pItemEmptyType(nullptr)
             , m_pItemType(nullptr)
             , m_pItemBool(nullptr)
             , m_pItemKey(nullptr)
             , m_pItemValue(nullptr)
             , m_iTreeStatus(TREE_DATA_STATUS_EMPTY)
{
    // 设置窗口标题和限制窗口大小
    setWindowTitle(QString("参数输入"));
    setFixedSize(600, 100);

    m_pItemKey = new QLineEdit(this);
    m_pItemValue = new QLineEdit(this);
    m_pItemType = new QComboBox(this);
    m_pItemBool = new QComboBox(this);
    m_pItemEmptyType = new QComboBox(this);
    m_pItemKey->setAlignment(Qt::AlignHCenter);
    m_pItemValue->setAlignment(Qt::AlignHCenter);
    m_pItemKey->setEnabled(false);
    m_pItemValue->setEnabled(false);

    QHBoxLayout* pLayoutInfo = new QHBoxLayout;
    addLayout2BaseLayout(pLayoutInfo);
    pLayoutInfo->setSpacing(4);

    // 添加输入框
    QGroupBox *pItemType = new QGroupBox(this);
    QVBoxLayout *pLayoutItemType = new QVBoxLayout;
    pItemType->setTitle(QString("数据类型"));
    pItemType->setFont(gstFont);
    pLayoutInfo->addWidget(pItemType);
    pItemType->setLayout(pLayoutItemType);
    pLayoutItemType->addWidget(m_pItemEmptyType);
    pLayoutItemType->addWidget(m_pItemType);

    connect(m_pItemType, SIGNAL(currentIndexChanged(int)), this, SLOT(currentIndexChanged(int)));

    // 设置类型与其对应的图标
    if(gmapJSONTypeIcon.isEmpty())
    {
        gmapJSONTypeIcon[QString::number(cJSON_False)] = QIcon(":/signals/resource/signals/circle_red.ico");
        gmapJSONTypeIcon[QString::number(cJSON_True)] = QIcon(":/signals/resource/signals/circle_green.ico");
        gmapJSONTypeIcon[QString::number(cJSON_NULL)] = QIcon(":/signals/resource/signals/json_null.ico");
        gmapJSONTypeIcon[QString::number(cJSON_Number)] = QIcon(":/signals/resource/signals/json_number.ico");
        gmapJSONTypeIcon[QString::number(cJSON_String)] = QIcon(":/signals/resource/signals/json_string.ico");
        gmapJSONTypeIcon[QString::number(cJSON_Array)] = QIcon(":/signals/resource/signals/json_array.ico");
        gmapJSONTypeIcon[QString::number(cJSON_Object)] = QIcon(":/signals/resource/signals/json_object.ico");
    }

    m_pItemType->addItem(QString("选择类型"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_Array)], QString("数组"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_Object)], QString("对象"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_NULL)], QString("空值"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_Number)], QString("数值"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_True)], QString("布尔值"));
    m_pItemType->addItem(gmapJSONTypeIcon[QString::number(cJSON_String)], QString("字符串"));

    m_pItemEmptyType->addItem(QString("选择类型"));
    m_pItemEmptyType->addItem(gmapJSONTypeIcon[QString::number(cJSON_Array)], QString("数组"));
    m_pItemEmptyType->addItem(gmapJSONTypeIcon[QString::number(cJSON_Object)], QString("对象"));

    // 设置选项高度
    m_pItemType->setFixedHeight(25);
    QStandardItemModel *pmodel1 = qobject_cast<QStandardItemModel*>(m_pItemType->model());
    for (int i = 0; i < pmodel1->rowCount(); ++i)
    {
        pmodel1->item(i)->setSizeHint({ 0, 25 });
    }

    m_pItemEmptyType->setFixedHeight(25);
    QStandardItemModel *pmodel2 = qobject_cast<QStandardItemModel*>(m_pItemEmptyType->model());
    for (int i = 0; i < pmodel2->rowCount(); ++i)
    {
        pmodel2->item(i)->setSizeHint({ 0, 25 });
    }

    m_pGroupKey = new QGroupBox(this);
    QVBoxLayout *pLayoutItemKey = new QVBoxLayout;
    m_pGroupKey->setTitle(QString("Key"));
    m_pGroupKey->setFont(gstFont);
    pLayoutInfo->addWidget(m_pGroupKey);
    m_pGroupKey->setLayout(pLayoutItemKey);
    pLayoutItemKey->addWidget(m_pItemKey);

    m_pGroupValue = new QGroupBox(this);
    QVBoxLayout *pLayoutItemValue = new QVBoxLayout;
    m_pGroupValue->setTitle(QString("Value"));
    m_pGroupValue->setFont(gstFont);
    pLayoutInfo->addWidget(m_pGroupValue);
    m_pGroupValue->setLayout(pLayoutItemValue);
    pLayoutItemValue->addWidget(m_pItemValue);
    pLayoutItemValue->addWidget(m_pItemBool);

    m_pItemBool->addItem(QString("   True    "));
    m_pItemBool->addItem(QString("   False   "));
    m_pItemBool->hide();

    addBaseFuncAction();
}

// 设置上层加载状态，每次启用时更新
void DialogAddItem::setTreeStatus(const uint iStatus)
{
    m_iTreeStatus = iStatus;
    if(TREE_DATA_STATUS_EMPTY == m_iTreeStatus)
    {
        // 空白，则限制进支持对象或数组输入
        m_pItemType->hide();
        m_pItemEmptyType->show();
        m_pGroupKey->hide();
        m_pGroupValue->hide();
        setFixedSize(250, 100);
    }
    else if(cJSON_Array == m_iTreeStatus)
    {
        // 数组，则数据仅支持对象，数值，字符串，布尔值和空值等内容，不具备key
        m_pItemType->show();
        m_pItemEmptyType->hide();
        m_pGroupKey->hide();
        m_pGroupValue->show();
        setFixedSize(600, 100);
    }
    else
    {
        // 已有数据，支持所有项目输入
        m_pItemType->show();
        m_pItemEmptyType->hide();
        m_pGroupKey->show();
        m_pGroupValue->show();
        setFixedSize(600, 100);
    }
}

QIcon DialogAddItem::getIconByJsonType(const QString& strType)
{
    QIcon iRet;
    auto iter = gmapJSONTypeIcon.find(strType);
    if(gmapJSONTypeIcon.end() != iter )
    {
        iRet = iter.value();
    }

    return iRet;
}

// 完成按钮功能
bool DialogAddItem::funcDone()
{
    QString strKey = m_pItemKey->text();
    QString strValue = m_pItemValue->text();
    uint iType = cJSON_Object;
    if(TREE_DATA_STATUS_EMPTY == m_iTreeStatus)
    {
        int iCurType = m_pItemEmptyType->currentIndex();
        if(0 == iCurType)
        {
            myMessageBoxShowOne(FORM_NULL, MSG_SHOW_ERROR, QString("请先选择初始节点数据类型"));
            return false;
        }
        else if(1 == iCurType)
        {
            iType = cJSON_Array;
        }
        else
        {
            iType = cJSON_Object;
        }

        // 初始根节点不需要键值
        strKey.clear();
        strValue.clear();
    }
    else
    {
        // 非对象数组的情况，则key是必须的
        if( cJSON_Array != m_iTreeStatus && strKey.isEmpty())
        {
            myMessageBoxShowOne(FORM_NULL, MSG_SHOW_ERROR, QString("请先输入键值或选择数据类型"));
            return false;
        }

        // 类型判断
        int iCurType = m_pItemType->currentIndex();
        switch (iCurType) {
        case 1:
            strValue.clear();
            iType = cJSON_Array;
            break;
        case 2:
            strValue.clear();
            iType = cJSON_Object;
            break;
        case 3:
            iType = cJSON_NULL;
            break;
        case 4:
            iType = cJSON_Number;
            // 需要对数值做有效性判断

            break;
        case 5:
            iType = (0 == m_pItemBool->currentIndex() ? cJSON_True : cJSON_False);
            strValue = (0 == m_pItemBool->currentIndex() ? QString("true") : QString("false"));
            break;
        case 6:
            iType = cJSON_String;
            break;
        default:
            myMessageBoxShowOne(FORM_NULL, MSG_SHOW_ERROR, QString("请先选择对象类型"));
            return false;   // 窗口不关闭
        }
    }

    emit sendMsg(iType, strKey, strValue);

    m_pItemKey->clear();
    m_pItemValue->clear();
    m_pItemEmptyType->setCurrentIndex(0);
    m_pItemType->setCurrentIndex(0);
    m_pItemBool->setCurrentIndex(0);
    m_pItemKey->setEnabled(false);
    m_pItemValue->setEnabled(false);

    return true;
}

// 完成取消功能
void DialogAddItem::funcCancel()
{
    m_pItemKey->clear();
    m_pItemValue->clear();
    m_pItemType->setCurrentIndex(0);
    m_pItemBool->setCurrentIndex(0);
    m_pItemKey->setEnabled(false);
    m_pItemValue->setEnabled(false);
}

// 类型选择框对应显示内容
void DialogAddItem::currentIndexChanged(int idx)
{
    switch (idx)
    {
    case 0:
        // 初始化状态
        m_pItemKey->setEnabled(false);
        m_pItemValue->setEnabled(false);
        break;
    case 1:
        // 数组
        m_pItemBool->hide();
        m_pItemKey->setEnabled(true);
        m_pItemValue->clear();
        m_pItemValue->show();
        m_pItemValue->setEnabled(false);
        m_pItemValue->setText(QString("[ ... ]"));
        break;
    case 2:
        // 对象
        m_pItemBool->hide();
        m_pItemKey->setEnabled(true);
        m_pItemValue->clear();
        m_pItemValue->show();
        m_pItemValue->setEnabled(false);
        m_pItemValue->setText(QString("{ ... }"));
        break;
    case 3:
        // 空值选择
        m_pItemBool->hide();
        m_pItemKey->setEnabled(true);
        m_pItemValue->clear();
        m_pItemValue->show();
        m_pItemValue->setText(QString("NULL"));
        m_pItemValue->setEnabled(false);
        break;
    case 4:
        // 数值
        m_pItemBool->hide();
        m_pItemKey->setEnabled(true);
        m_pItemValue->setEnabled(true);
        m_pItemValue->clear();
        m_pItemValue->show();
        break;
    case 5:
        // 布尔值选择
        m_pItemKey->setEnabled(true);
        m_pItemValue->hide();
        m_pItemBool->show();
        m_pItemBool->setCurrentIndex(0);
        break;
    case 6:
        // 字符串
        m_pItemBool->hide();
        m_pItemKey->setEnabled(true);
        m_pItemValue->setEnabled(true);
        m_pItemValue->clear();
        m_pItemValue->show();
        break;
    default:
        break;
    }
}